package com.zt.service.impl;

import cn.hutool.jwt.JWT;
import cn.hutool.jwt.JWTUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zt.domain.Orders;
import com.zt.domain.PriceLess;
import com.zt.domain.Shops;
import com.zt.domain.Xq;
import com.zt.dto.BuyDto;
import com.zt.dto.TransDto;
import com.zt.mapper.PriceLessMapper;
import com.zt.mapper.ShopsMapper;
import com.zt.mapper.XqMapper;
import com.zt.service.OrdersService;
import com.zt.mapper.OrdersMapper;
import com.zt.service.ShopsService;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
* @author Lenovo
* @description 针对表【orders】的数据库操作Service实现
* @createDate 2025-05-09 15:04:09
*/
@Service
public class OrdersServiceImpl extends ServiceImpl<OrdersMapper, Orders>
    implements OrdersService{
    @Resource
    private RedisTemplate<String ,Object> redisTemplate;
    @Resource
    private ShopsMapper shopsMapper;
    @Resource
    private PriceLessMapper priceLessMapper;
    @Resource
    private OrdersMapper ordersMapper;
    @Resource
    private XqMapper xqMapper;
    @Override
    public void create(TransDto transDto) {
        Object token = redisTemplate.opsForValue().get("token");
        System.out.println(token.toString());
        JWT jwt = JWTUtil.parseToken(token.toString());
        String id = jwt.getPayload("id").toString();
        System.err.println(id);
        BigDecimal total=new BigDecimal(0);
        List<BuyDto> buyDto = transDto.getBuyDto();
        List<Integer> list = buyDto.stream().map(t -> t.getId()).collect(Collectors.toList());
        QueryWrapper<Shops> shopsQueryWrapper = new QueryWrapper<>();
        shopsQueryWrapper.in("id",list);
        List<Shops> shops = shopsMapper.selectList(shopsQueryWrapper);
//        Map<Integer,Shops> map=new HashMap<>();
//        for (Shops shop : shops) {
//            map.put(shop.getId(),shop);
//        }
        Map<Integer, Shops> collect = shops.stream().collect(Collectors.toMap(Shops::getId, t -> t));
        for (BuyDto buy : buyDto) {
            Shops shop = collect.get(buy.getId());
            BigDecimal multiply = shop.getPrice().multiply(new BigDecimal(buy.getNum()));
            total=total.add(multiply);
        }
        Orders orders=new Orders();
        PriceLess priceLess = priceLessMapper.selectById(1);
        BigDecimal subtract = total.subtract(priceLess.getLessPrice());
        orders.setPrice(subtract);
        orders.setUserId(Integer.valueOf(id));
        ordersMapper.insert(orders);
        priceLess.setNum(priceLess.getNum()-1);
        priceLessMapper.updateById(priceLess);
        List<Xq> xqList=new ArrayList<>();
        for (BuyDto buy : buyDto) {
            Shops shop = collect.get(buy.getId());
            BigDecimal multiply = shop.getPrice().multiply(new BigDecimal(buy.getNum()));
            Xq xq=new Xq();
            xq.setOrderId(orders.getId());
            xq.setShopId(shop.getId());
            xq.setPrice(multiply);
            xqMapper.insert(xq);
        }
    }
}




